/**
 * @a https://leetcode.cn/problems/map-of-highest-peak/
 */

#include "../../common.h"

class Solution {
    vector<vector<bool>> vis;
public:
    vector<vector<int>> highestPeak(vector<vector<int>>& isWater) {
        const static int dx[4] = {0,0,1,-1};
        const static int dy[4] = {1,-1,0,0};
        int row = isWater.size(), col = isWater[0].size();
        vis.resize(row, vector<bool>(col, false));

        queue<pair<int,int>> que;
        vector<vector<int>> ret(row,vector<int>(col));
        for(int i = 0; i < row; ++i){
            for(int j = 0; j < col; ++j){
                if(isWater[i][j])
                {
                    que.push({i,j});
                    vis[i][j] = true;
                    ret[i][j] = 0;
                }
            }
        }
        int step = 1;
        while(que.size()){
            int sz = que.size();
            while(sz--){
                auto [a,b] = que.front(); que.pop();
                for(int i = 0; i < 4; ++i){
                    int x = a + dx[i], y = b + dy[i];
                    if(x >= 0 && x < row && y >= 0 && y < col
                        && vis[x][y] == false) // isWater[x][y] == 0 always true;
                    {
                        que.push({x,y});
                        vis[x][y] = true;
                        ret[x][y] = step;
                    }
                }
            }
            step++;
        }
        return ret;
    }
};